﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;


public partial class admin_Login : System.Web.UI.Page
{
    //private int expireTime = 15;

    protected void Page_Load(object sender, EventArgs e)
    {
        txtMessage.Text = "";
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            String username = txtUserName.Text;
            String passw = txtPassword.Text;
            bool isRem = cbRemember.Checked;

            byte[] input = Encoding.ASCII.GetBytes(passw);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(input);
            string passEncoding = Encoding.ASCII.GetString(output);

            // check admin
            DataTable admin = AdminManagement.GetUser(username, passEncoding).Tables["Admin"];
            if (admin.Rows.Count > 0)
            {
                // login successfully (admin)
                Session["username"] = username;
                String fullN = admin.Rows[0]["adminName"].ToString();
                Session["fullname"] = fullN;
                //if (isRem)
                //{
                //    // remember 
                //    HttpCookie cookie = new HttpCookie("username", username);
                //    cookie.Expires = DateTime.Today.AddDays(expireTime);
                //    Response.Cookies.Add(cookie);

                //    cookie = new HttpCookie("fullname", fullN);
                //    cookie.Expires = DateTime.Today.AddDays(expireTime);
                //    Response.Cookies.Add(cookie);
                //}
                if (Request.QueryString["prePage"] != null)
                {
                    switch (Request.QueryString["prePage"].ToString())
                    {
                        case "adminPage": Response.Redirect("AdminManager.aspx");
                            break;
                        case "cusManager": Response.Redirect("CustomerManager.aspx");
                            break;
                        case "hotelManager": Response.Redirect("HotelManager.aspx");
                            break;
                        case "landManager": Response.Redirect("LandscapesManager.aspx");
                            break;
                        case "orderTour": Response.Redirect("OrderTour.aspx");
                            break;
                        case "personInfo": Response.Redirect("PersonInfo.aspx");
                            break;
                        default: Response.Redirect("TourManager.aspx");
                            break;
                    }
                }
                else
                    Response.Redirect("TourManager.aspx");

                ((LinkButton)Page.Parent.FindControl("btnLogout")).Visible = true;
            }
            else
            {
                // LOGIN FAILD
                ClientScript.RegisterStartupScript(typeof(Page), "error", "<script>alert('Invalid username or password!');</script>");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        txtUserName.Text = "";
        txtPassword.Text = "";

        txtUserName.Focus();
    }
}